<?php

class ProductTable extends Doctrine_Table
{
	/**
     * Método que retorna los Productos destacados para le home
     */
	public static function getProductsForHome(){
		return Doctrine::getTable('Product')
      				->createQuery('p')
      				->orderBy('p.name')
      				->limit(8)
      				->execute();
	}

	public static function getProductsForSite($site_id){
		return Doctrine::getTable('Product')
      				->createQuery('p')
      				->where('site_id = ?', $site_id)
      				->orderBy('p.name')
      				->limit(8)
      				->execute();
	}

    public static function getProductBySlug($slug){
        return  Doctrine::getTable('Product')->findOneBy('slug', $slug);
    }

	public static function getProductsByCategory($category){
		$category = Doctrine::getTable('Category')->findOneBy('name', $category);
        return  Doctrine::getTable('Product')->findBy('category_id', $category->getId());
    }

    public static function getSearchQuery($searchOptions)
    {
    	$query = new Doctrine_Query();
		$query->from("Product p");
		if (!empty($searchOptions["name"])) {
			$query->orWhere("p.name LIKE ?", "%".$searchOptions["name"]."%");
		}
		if (!empty($searchOptions["description"])) {
			$query->orWhere("p.description LIKE ?", "%".$searchOptions["description"]."%");
		}
		if (!empty($searchOptions["tags"])) {
		}
    	if (!empty($searchOptions['category_id'])) {
    		//die($searchOptions['category_id']);
    		$query->innerJoin("p.Site s");
			$query->innerJoin("s.Category c");
			$query->addWhere("c.id = ".$searchOptions['category_id']);
		}
		return $query;
    }

	public static function search($searchOptions)
	{
		//return $products;
		$query = new Doctrine_Query();
		$query->from("Product p");
		if (!empty($searchOptions["name"])) {
			$query->orWhere("p.name LIKE ?", "%".$searchOptions["name"]."%");
		}
		if (!empty($searchOptions["description"])) {
			$query->orWhere("p.description LIKE ?", "%".$searchOptions["description"]."%");
		}
		if (!empty($searchOptions["tags"])) {
		}
		if (!empty($searchOptions['category_id'])) {
			$query->leftJoin("p.Category c");
			$query->addWhere("c.id = ".$searchOptions["category_id"]);
		}
		return $query->execute();
	}

	public static function searchByTags($searchString)
	{
		$tagsArray = explode(' ', $searchString);
		return Doctrine::getTable('ProductTag')
					->createQuery('pt')
					->leftJoin('pt.Product p')
					->leftJoin('pt.Tag t')
					->whereIn('t.name', $tagsArray)
					->execute();
	}

	static public function getLuceneIndex()
	{
		ProjectConfiguration::registerZend();
		if (file_exists($index = self::getLuceneIndexFile())) {
			return Zend_Search_Lucene::open($index);
		}
		return Zend_Search_Lucene::create($index);
	}

	static public function getLuceneIndexFile()
	{
		return sfConfig::get('sf_data_dir').'/indexes/product.'.sfConfig::get('sf_environment').'.index';
	}

	public function getForLuceneQuery($query)
	{
		$hits = self::getLuceneIndex()->find($query);
		$pks = array();
		foreach ($hits as $hit) {
			$pks[] = $hit->pk;
		}
		if (empty($pks)) {
			return array();
		}
		$q = $this->createQuery('p')
				->whereIn('j.id', $pks)
				->limit(20);
		//$q = $this->addActiveJobsQuery($q);
		return $q->execute();
	}
}